package net.huashitong.oversee.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.filter.NewProjectsFilter;
import net.huashitong.node.filter.NodeNewschedulesFilter;
import net.huashitong.node.service.NodeNewschedulesService;
import net.huashitong.oversee.entity.SupervisionTask;
import net.huashitong.oversee.filter.CoordinateFilter;
import net.huashitong.oversee.filter.SupervisionTaskFilter;
import net.huashitong.oversee.service.CoordinateService;
import net.huashitong.oversee.service.SupervisionTaskService;
import net.huashitong.user.databean.OnLineDatabean;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.user.filter.OnlineFilter;
import net.huashitong.user.service.LoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.util.List;

import static net.huashitong.utils.UtilsTools.getCurrentDateTime1;
import static net.huashitong.utils.UtilsTools.getLastMonthDateTime;

@Controller
@RequestMapping("/reportForm4")
public class ReportForm4Ctrl extends JsonBaseController {
    @Override
    protected String getView(String s) {
        return "/modules/reportForm4/" + s;
    }

    @Autowired
    private LoginLogService loginLogService;
    @Autowired
    private SupervisionTaskService supervisionTaskService;
    @Autowired
    private CoordinateService coordinateService;
    /*@Autowired
    private ExportService exportService;*/

    @Autowired
    private NodeNewschedulesService nodeNewschedulesService;

    @RequestMapping(method = RequestMethod.GET)
    public String initPage(HttpServletRequest request, Model model) {
        model.addAttribute("leftType", "报表模块");
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        if (ObjectUtils.isBlank(bnpUser)) {
            return "/modules/login/login";
        }
        //待办任务个数
        int nodeCount = (int) WebUtils.getSessionAttribute(request, "nodeCount");
        model.addAttribute("nodeCount", nodeCount);
        model.addAttribute("user",bnpUser);

        int myTaskNum = (int) WebUtils.getSessionAttribute(request, "myTaskNum");
        int quetionNum = (int) WebUtils.getSessionAttribute(request, "quetionNum");
        model.addAttribute("myTaskNum", myTaskNum);
        model.addAttribute("quetionNum", quetionNum);

        int myTackTaskNum = (int) WebUtils.getSessionAttribute(request, "myTackTaskNum");
        model.addAttribute("myTackTaskNum", myTackTaskNum);

        return getView("query");
    }


    //初始化数据
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute OnlineFilter filter) {
        //默认查询本月
        if(StringUtils.isBlank(filter.getStartDate())){
            filter.setStartDate(getLastMonthDateTime().replace("-","."));
        }
        if(StringUtils.isBlank(filter.getEndDate())){
            filter.setEndDate(getCurrentDateTime1());
        }

        List<OnLineDatabean> list = loginLogService.queryUserOnlineList(filter);
        //统计进行中的节点
        for(OnLineDatabean bean : list){
            //进行中的节点
            filter.setName(bean.getName());
            BigInteger progressNodes =  nodeNewschedulesService.queryProgressNodesCountByFilter(filter);
            bean.setProgressNodes(progressNodes+"");
            //进行中的督查任务数量
            /*List<SupervisionTask> tasks =  supervisionTaskService.getDoing(bean.getName(),bean.getRole());
            BigInteger taskBigInt = new BigInteger(tasks.size()+"");
            bean.setProgressNodes(progressNodes.add(taskBigInt)+"");*/
            bean.setQueryTime(filter.getStartDate()+"--"+filter.getEndDate());
        }

        return coverJqGrid(filter, list);
    }

    @RequestMapping("/filter")
    public String filter(@ModelAttribute NewProjectsFilter filter, Model model) {
        model.addAttribute("newProjectsFilter", filter);
        return getView("filter");
    }

    @RequestMapping("/formHead")
    public String showHead(@ModelAttribute NodeNewschedulesFilter filter, Model model) {
        model.addAttribute("taskjob", filter);
        return getView("formHead");
    }

}
